// Vertex shader program
var VSHADER_SOURCE =
  "attribute vec4 a_Position;\n" + // attribute variable
  "attribute float a_PointSize;\n" +
  "void main() {\n" +
  "  gl_Position = a_Position;\n" +
  "  gl_PointSize = a_PointSize;\n" +
  "}\n";

// Fragment shader program
var FSHADER_SOURCE =
  "void main() {\n" + "  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" + "}\n";

function main() {
  // Retrieve <canvas> element
  var canvas = document.getElementById("webgl");

  // Get the rendering context for WebGL
  var gl = getWebGLContext(canvas);
  if (!gl) {
    console.log("Failed to get the rendering context for WebGL");
    return;
  }

  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { //创建程序对象
    console.log("Failed to intialize shaders.");
    return;
  }

  //获取attribute的变量的存储位置
  var a_Position = gl.getAttribLocation(gl.program, "a_Position");
  var a_PointSize = gl.getAttribLocation(gl.program, "a_PointSize")

  if (a_Position < 0) {
    return;
  }
  //将顶点位置传输给attribute变量
  gl.vertexAttrib4f(a_Position, 0.5, 0.0, 1.0, 1.0);
  gl.vertexAttrib1f(a_PointSize, 30.0);
  gl.clearColor(0.0, 0.0, 0.0, 1.0);

  // Clear <canvas>
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Draw
  gl.drawArrays(gl.POINTS, 0, 1);
}
